Ext.define('Emp.controller.ResourceController', {
    extend: 'Emp.controller.BaseController',

    models: ['Resource'],
    views: ['resource.List', 'resource.Edit', 'resource.OperatorSelect'],
    refs: [
        {ref: 'resourceList', selector: 'resourcelist'},
        {ref: 'resourceEdit', selector: 'resourceedit', xtype: 'resourceedit', autoCreate: true},
        {ref: 'operatorSelect', selector: 'operatorselect', xtype: 'operatorselect', autoCreate: true}
    ],
    
    init: function() {
        this.control({
        	'resourcelist treepanel': {
        		'itemclick': this.doClickOnTree
        	},
        	'resourcelist grid': {
        		'itemdblclick': this.doModifyOnList
        	},
            'resourcelist gridtoolbar button[itemId=create]': {
                'click': this.doCreateOnList
            },
            'resourcelist gridtoolbar button[itemId=modify]': {
                'click': this.doModifyOnList
            },
            'resourcelist gridtoolbar button[itemId=read]': {
                'click': this.doReadOnList
            },
            'resourcelist gridtoolbar button[itemId=remove]': {
                'click': this.doRemoveOnList
            },
            'resourcelist gridtoolbar button[itemId=config]': {
                'click': this.doConfig
            },
            'resourceedit': {
                'afterrender': this.doLoadOnEdit
            },
            'resourceedit button[action=save]': {
                'click': this.doSaveOnEdit
            },
            'resourceedit menuitem[action=save_close]': {
                'click': this.doSaveCloseOnEdit
            },
            'resourceedit button[action=create]': {
                'click': this.doCreateOnEdit
            },
            'resourceedit button[action=close]': {
                'click': this.doCloseOnEdit
            }
        });
    },
    
    doConfig: function() {
    	var record = this.getResourceList().grid.getSelectionModel().getSelection()[0];
		if (record) {
			var id = record.get(record.idProperty);
			var edit = this.getOperatorSelect({resource_id: id});
			this.getResourceList().add(edit);
			edit.show();
		} else {
			Ext.show.msg('提示', '请选择数据');
		}
    },
    
	doClickOnTree: function(view, record, item, index, e, options) {
		var store = this.getResourceList().down('grid').store,
			filter = new Array(),
			id = record.get('id');
		filter.push({
			property: 'parentId',
			value: id
		});
		store.proxy.extraParams = {
			filter: Ext.encode(filter)
		};
		store.load({start: 0});
	},
	
	doBeforeCreateOnList: function(edit) {
		var record = this.getResourceList().tree.getSelectionModel().getSelection()[0];
		if (record) {
			var parentId = record.get(record.idProperty);
			edit.down('combotree[name=parentId]').setValue(parentId);
		}
	}
});